<?php

	class formidable_mainobject {

		var $oForm			= null;
		var $aElement		= null;
		var $sExtPath		= null;
		var $sExtRelPath	= null;
		var $sExtWebPath	= null;
		var $aObjectType	= null;
		
		var $sXPath			= null;

		var $sNamePrefix = FALSE;

		function _init(&$oForm, $aElement, $aObjectType, $sXPath, $sNamePrefix = FALSE) {
			
			$this->oForm =& $oForm;
			$this->aElement = $aElement;
			$this->aObjectType = $aObjectType;

			$this->sExtPath = $aObjectType["PATH"];
			$this->sExtRelPath = $aObjectType["RELPATH"];
			$this->sExtWebPath = t3lib_div::getIndpEnv("TYPO3_SITE_URL") . $this->sExtRelPath;

			$this->sXPath = $sXPath;

			$this->sNamePrefix = $sNamePrefix;

			if(is_array($this->oForm->conf[$aObjectType["OBJECT"] . "."]) && array_key_exists($aObjectType["EXTKEY"] . ".", $this->oForm->conf[$aObjectType["OBJECT"] . "."])) {
				$this->conf = $this->oForm->conf[$aObjectType["OBJECT"] . "."][$aObjectType["EXTKEY"] . "."];
			} else {
				$this->conf = array();
			}
		}

		function _getType() {
			return $this->aElement["type"];
		}

		function _navConf($sPath, $aConf = FALSE) {
			
			if($aConf !== FALSE) {
				return $this->oForm->_navConf($sPath, $aConf);
			}

			return $this->oForm->_navConf($sPath, $this->aElement);
		}

		/**
		 * [Describe function...]
		 *
		 * @param	[type]		$sPath: ...
		 * @param	[type]		$aConf: ...
		 * @return	[type]		...
		 */
		function _isTrue($sPath) {

			return $this->_isTrueVal(
				$this->_navConf(
					$sPath
				)
			);

		}

		function isTrue($sPath) {
			return $this->_isTrue($sPath);
		}

		/**
		 * [Describe function...]
		 *
		 * @param	[type]		$sPath: ...
		 * @param	[type]		$aConf: ...
		 * @return	[type]		...
		 */
		function _isFalse($sPath) {

			$mValue = $this->_navConf($sPath);

			if($mValue !== FALSE) {
				return $this->_isFalseVal($mValue);
			} else {
				return FALSE;	// if not found in conf, the searched value is not FALSE, so _isFalse() returns FALSE !!!!
			}
		}

		function isFalse($sPath) {
			return $this->_isFalse($sPath);
		}

		/**
		 * [Describe function...]
		 *
		 * @param	[type]		$mVal: ...
		 * @return	[type]		...
		 */
		function _isTrueVal($mVal) {

			if($this->oForm->isRunneable($mVal)) {
				$mVal = $this->callRunneable($mVal);
			}

			return (($mVal === TRUE) || ($mVal == "1") || (strtoupper($mVal) == "TRUE"));
		}

		function isTrueVal($mVal) {
			return $this->_isTrueVal($mVal);
		}

		/**
		 * [Describe function...]
		 *
		 * @param	[type]		$mVal: ...
		 * @return	[type]		...
		 */
		function _isFalseVal($mVal) {

			if($this->oForm->isRunneable($mVal)) {
				$mVal = $this->callRunneable($mVal);
			}

			return (($mVal == FALSE) || (strtoupper($mVal) == "FALSE"));
		}

		function isFalseVal($mVal) {
			return $this->_isFalseVal($mVal);
		}

		/**
		 * [Describe function...]
		 *
		 * @param	[type]		$sPath: ...
		 * @param	[type]		$aConf: ...
		 * @return	[type]		...
		 */
		function _defaultTrue($sPath) {

			if($this->_navConf($sPath) !== FALSE) {
				return $this->_isTrue($sPath);
			} else {
				return TRUE;	// TRUE as a default
			}
		}

		/**
		 * [Describe function...]
		 *
		 * @param	[type]		$sPath: ...
		 * @param	[type]		$aConf: ...
		 * @return	[type]		...
		 */
		function _defaultFalse($sPath) {

			if($this->_navConf($sPath) !== FALSE) {
				return $this->_isTrue($sPath);
			} else {
				return FALSE;	// FALSE as a default
			}
		}

		// alias for _defaultTrue()
		function defaultTrue($sPath) {
			return $this->_defaultTrue($sPath);
		}

		// alias for _defaultFalse()
		function defaultFalse($sPath) {
			return $this->_defaultFalse($sPath);
		}


		/**
		 * [Describe function...]
		 *
		 * @param	[type]		$sPath: ...
		 * @param	[type]		$aConf: ...
		 * @return	[type]		...
		 */
		function _defaultTrueMixed($sPath) {

			if(($mMixed = $this->_navConf($sPath)) !== FALSE) {
				
				if(strtoupper($mMixed) !== "TRUE" && strtoupper($mMixed) !== "FALSE") {
					return $mMixed;
				}

				return $this->_isTrue($sPath);
			} else {
				return TRUE;	// TRUE as a default
			}
		}

		function defaultTrueMixed($sPath) {
			return $this->_defaultTrueMixed($sPath);
		}

		/**
		 * [Describe function...]
		 *
		 * @param	[type]		$sPath: ...
		 * @param	[type]		$aConf: ...
		 * @return	[type]		...
		 */
		function _defaultFalseMixed($sPath) {

			if(($mMixed = $this->_navConf($sPath)) !== FALSE) {
				
				if(strtoupper($mMixed) !== "TRUE" && strtoupper($mMixed) !== "FALSE") {
					return $mMixed;
				}

				return $this->_isTrue($sPath);
			} else {
				return FALSE;	// FALSE as a default
			}
		}

		function defaultFalseMixed($sPath) {
			return $this->_defaultFalseMixed($sPath);
		}




		// this has to be static !!!
		function loaded(&$aParams) {
		}

		function cleanBeforeSession() {
			$this->baseCleanBeforeSession();
			unset($this->oForm);
		}

		function baseCleanBeforeSession() {
			/*unset($this->oForm);*/
		}

		function awakeInSession(&$oForm) {
			$this->oForm =& $oForm;
		}

		function setParent(&$oParent) {
			/* nothing in main object */
		}
		
		function callRunneable($mMixed, $aParams = array(), $aItems = FALSE, $oRef = FALSE, $bAddDots = TRUE) {
			
			if(($sContext = $this->_navConf("/userobj/php/context", $mMixed)) === "relative") {
				//debug("locaaaaaal");
				if(($mPhp = $this->_navConf("/userobj/php", $mMixed)) !== FALSE) {

					if(is_array($mPhp) && array_key_exists("__value", $mPhp)) {
						$sPhp = $mPhp["__value"];
					} else {
						$sPhp = $mPhp;
					}

					$sClassName = uniqid("tempcl");
					$sMethodName = uniqid("tempmet");

					$this->__sEvalTemp = array("code" => $sPhp, "xml" => $mMixed);

					$GLOBALS['formidable_tempuserobj'] =& $this;
					$sPhp = str_replace("\$this", "\$GLOBALS['formidable_tempuserobj']", $sPhp);

					$sClass =	"class " . $sClassName . " {"
						.	"	function " . $sMethodName . "(\$_this, \$aParams) { \$_this=&\$GLOBALS['formidable_tempuserobj'];"
						.	"		" . $sPhp
						.	"	}"
						.	"}" ;

					//set_error_handler(array(&$this, "__catchEvalException"));
					eval($sClass);
					$oObj = new $sClassName();

					$this->oForm->pushUserObjParam($aParams);

					$sRes = call_user_func(
						array(
							&$oObj,
							$sMethodName
						),
						$this,
						$aParams
					);

					$this->oForm->pullUserObjParam();

					unset($this->__sEvalTemp);
					//restore_error_handler();

					return $sRes;

				}
			}
			
			return $this->oForm->callRunneable(
				$mMixed,
				$aParams,
				$aItems,
				$oRef,
				$bAddDots
			);
		}
	}
	
	if (defined("TYPO3_MODE") && $TYPO3_CONF_VARS[TYPO3_MODE]["XCLASS"]["ext/ameos_formidable/api/class.mainobject.php"])	{
		include_once($TYPO3_CONF_VARS[TYPO3_MODE]["XCLASS"]["ext/ameos_formidable/api/class.mainobject.php"]);
	}
?>